package models

import (
	"github.com/jinzhu/gorm"
	"hytx_media/pkg/util"
	"time"
	"fmt"
)

type Comments struct {
	gorm.Model
	Aid int
	UserId int
	Content string
}

func (Comments) TableName() string {
	return "comments"
}

type CommentsResult struct {
	ID int `json:"id"`
	Content string `json:"content"`
	WechatName string `json:"name"`
	HeadPortrait string `json:"head_portrait"`
	IsStick bool `json:"is_stick"`
	CreatedAt time.Time `json:"created_at"`
}

type CommentsNumResult struct {
	ID int `json:"id"`
	Title string `json:"title"`
	Num int `json:"num"`
	NewestAt time.Time `gorm:"column:last_comment_at" json:"newest_at"`
}

/**
	获取文章评论列表
 */
func GetCommentsByUser(page int, limit int, id int, userId int) (map[string]interface{}, *util.Paginate, error) {

	var comments []*CommentsResult
	var count int
	row := DB.Table("articles").Select("title").Where("uuid=?", id).Row()
	var title string
	row.Scan(&title)
	DB.Model(&Comments{}).Where("a_id=?",id).Count(&count)
	paginate := util.Paginate{Page:page,Limit:limit,Count:count}
	err := DB.Table("comments m").Select("m.id,m.content, u.wechat_name as `name`, u.head_portrait, m.created_at,m.is_stick").Joins("left join user_detail u on m.user_id=u.user_id").Where("m.a_id=?", id).Order("m.is_stick desc,m.created_at desc").Offset(paginate.Offset()).Limit(paginate.Limit).Scan(&comments).Error
	if err != nil {
		return nil, nil, err
	}
	result := map[string]interface{}{"title":title,"comments":comments}
	fmt.Print(paginate)
	return result, &paginate, nil
}
/**

 */
func GetCommentNumList(page int, limit int, userId int) ([]*CommentsNumResult, *util.Paginate, error) {
	var result []*CommentsNumResult
	var count int
	DB.Table("articles").Where("author=?", userId).Count(&count)
	paginate := util.Paginate{Page:page, Limit:limit, Count:count}
	err := DB.Table("articles").Select("id,title,comment_num num,last_comment_at").Where("author=?", userId).Offset(paginate.Offset()).Limit(paginate.Limit).Scan(&result).Error
	if err != nil {
		return nil, nil, err
	}
	return result, &paginate, err
}

type CommentFeedback struct {
	ID int `json:"id"`
	UserId int `json:"user_id"`
	MediaUserId	int `json:"media_user_id"`
	CId int `json:"c_id"`
	Type string `json:"type"`
	Content string `json:"content"`
	CreatedAt time.Time `json:"created_at"`
}

